<?php

namespace framework\system\kernel;

use framework;

class Route extends Component {

    private $config;
    private $url;
    public $gets = [];
    public $controllerId = null;
    public $actionId = null;

    public function init() {


        //framework::console()->sendEvent(["event.web.urlManager.analysisGet"]);
        //framework::console()->sendEvent(["event.web.urlManager.getPath"]);
        //framework::console()->sendEvent(["event.web.urlManager.route"]);
        //framework::console()->sendEvent(["event.web.urlManager.analysisPath"]);
        //framework::console()->sendEvent(["event.web.urlManager.analysisGet"]);
        //framework::console()->sendEvent(["event.web.urlManager.getController"]);
        //framework::console()->sendEvent(["event.web.urlManager.getAction"]);
    }

    public function start() {
        $cmp_config = framework::cm()->get('com.config');
        $cmp_request = framework::cm()->get('com.request');
        $this->config['rules'] = $cmp_config->get('appliaction.route.rules');
        $this->config['ext'] = $cmp_config->get('appliaction.route.ext');
        $this->config['delimiter'] = $cmp_config->get('appliaction.route.delimiter');
        $path_info = $cmp_request->getPathInfo();
        $request_url = $cmp_request->getRequestURL();
        $this->url = $path_info ? $path_info : $request_url;
        $this->url = str_replace('\\', '/', $this->url);
        framework::em()->push("event.route.rules");
        framework::em()->push("event.route.analysisPath");
        framework::em()->push("event.route.dispatch");
    }

    public function end() {
        ;
    }

    public function rules() {

        if (!empty($this->config['rules'])) {

            foreach ($this->config['rules'] as $rule) {
                if ($rule["search"]) {
                    $this->url = preg_replace("#" . $rule["search"] . "#", $rule["replace"], $this->url);
                }
            }
        }
    }

    public function analysisPath() {


        //获得？前路径
        $path_info = stristr($this->url, '?');
        $path_info = str_replace($path_info, '', $this->url);
        if ($this->config['ext']) {
            $path_info = preg_replace("/." . $this->config['ext'] . "$/", '', $path_info, 1);
        }
        //获得CONTROLLER 和 ACTION
        $action_path = '';
        $params_path = '';
        $search = stristr($path_info, "." . $this->config['delimiter']);
        if ($search) {
            $action_path = str_replace($search, '', $path_info);
            $params_path = str_replace("." . $this->config['delimiter'], '', $search);
        } else {
            $action_path = $path_info;
        }

        if (substr($action_path, 0, 1) == '/') {
            $action_path = substr($action_path, 1);
        }
        if (substr($action_path, -1, 1) == '/') {
            $action_path = substr($action_path, 0, -1);
        }
        //echo "\r\n" . $action_path . "\r\n";
        $action_path = explode('/', $action_path);
        if (count($action_path) < 2) {
            if ($search) {
                $this->actionId = array_pop($action_path);
                $this->controllerId = framework::cm()->get('com.config')->get("application.defaultControllerId");
            } else {
                $this->actionId = framework::cm()->get('com.config')->get("application.defaultActionId");
                $this->controllerId = implode('/', $action_path);
            }
        } else {

            $this->actionId = array_pop($action_path);
            $this->controllerId = implode('/', $action_path);
        }

        preg_match_all("/[?|&]([A-Za-z0-9\_\-]+)=([A-Za-z0-9\:\_\%\.\-\/]+)/", $this->url, $OUT, PREG_SET_ORDER);
        for ($i = 0; !empty($OUT[$i]); $i++) {
            $_GET[$OUT[$i][1]] = urldecode($OUT[$i][2]);
        }
        if ($params_path) {
            $params = explode('/', $params_path);
            $params = array_filter($params);
            /*
             * 数字关联
             */

            foreach ($params as $key => $value) {
                $_GET['key_' . $key] = $value;
            }
            /*
             * 前后关联
             * 
             */
            for ($i = 1; isset($params[$i]); $i++) {
                $key = $params[$i];
                $value = isset($params[$i + 1]) ? $params[$i + 1] : null;
                if (!array_key_exists($key, $_GET) && !is_null($key)) {
                    $_GET[$key] = $value;
                }
            }
        }
        //print_r($_GET);
    }

    public function dispatch() {
        if (!$this->controllerId) {
            $this->controllerId = isset($_GET['controller']) ? $_GET['controller'] : NULL;
        }
        if (!$this->actionId) {
            $this->actionId = isset($_GET['action']) ? $_GET['action'] : NULL;
        }
        framework::mm()->send("message.application.controllerId", $this->controllerId);
        framework::mm()->send("message.application.actionId", $this->actionId);
    }

}
